<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>规则集管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		/* $(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		}); */
	
    
    function preview(){
    	var rel=$("#sel_relation").val();
    	var num=$("#sel_num").val();
    	var re = getRelation(rel);
    	var str='';
    	if(num>0){
    		for (var i=1;i<=num;i++){
    			  var name=$("#sel_name"+i).val();
    			  var nexus=$("#sel_nexus"+i).val();
    			  var remark=$("#sel_ruleSet"+i).val();
    			  var na=name.split('+');
    			  var nex=getNexus(nexus);
    			  if(nex==0){
    				  str+=na[1]+remark; 
    			  }else{
    			      str+=na[1]+nex+remark;
    			  }
    			  if(num>1 && i!=num){
    				  str=str+","+"\n"+re+"\n"; 	
    			    }    					
    		 }
           $("#input_remarks").val(str);
       }
    }
    
    function getNexus(str){
    	if(str==1){
			str=">=";
		}else if(str==2){
			str="<=";
		}else if(str==3){
			str="="
		}else if(str==4){
			str="包含"
		}else if(str==5){
			str="不包含"
		}
    	
    	return str;
    }
    
    
    function getRelation(str){
    	 if(str==1){
    		 str="或者";
    	 }else if(str==2){
    		 str="并且";
    	 }
    	 
    	 return str;
    }
    
    $(function(){  
       var ruleCon='${aegisRuleSet.ruleconfig}';
       var len =eval(ruleCon).length;
       var rul=eval(ruleCon);
       $("#sel_num").val(len);
       if(ruleCon!=''){
    	   if(len>1){
    		  setRuleConfig(rul);
    		  for(i=1;i<len;i++){
    	           var j=i+1;
   			        $("#"+i).append("<p><div class='controls;div-left;' id='"+j+"'>"+
   				       "<select  name='rules["+i+"].keyName' class='input-xlarge required' id='sel_name"+j+"' disabled='true'>"+
   				         "<c:forEach items='${keyword}' var='key' varStatus='i' >"+
   				          "<option value='${key.id}+${key.keywordname}+${key.type}'>${key.keywordname}</option>"+
   				          "</c:forEach>"+
   			           "</select>"+
   			           "    <select name='rules["+i+"].nexus' class='input-xlarge required' id='sel_nexus"+j+"' disabled='true'>"+
   			                "<option  value='0'></option>"+
   			                "<option  value='1'>>=</option>"+
   						    "<option  value='2'>&lt;=</option>"+
   						    "<option  value='3'>=</option>"+
   						    "<option  value='4'>包含</option>"+
   						    "<option  value='5'>不包含</option>"+
   			           "<select>"+
   			           "  <input name='rules["+i+"].ruleSet' htmlEscape='false' maxlength='20' class='input-xlarge required' id='sel_ruleSet"+j+"' readonly='true'/>"+
   			           "</div></P>");
   			          $("#sel_name"+j).val(rul[i].keyId+"+"+rul[i].keyName+"+"+rul[i].keyType);
 			          $("#sel_nexus"+j).val(rul[i].nexus);
 			          $("#sel_ruleSet"+j).val(rul[i].ruleSet);
 			          $("#aa"+i).remove();
    	             }
    	         }else if(len==1){
    	        	 setRuleConfig(rul);
    	         }  
    	      preview();//调用预览
            }
       })
    
    
    function setRuleConfig(rul){
    	$("#1").html("<div class='controls;div-left;' id='1'>"+
			       "<select  name='rules[0].keyName' class='input-xlarge required' id='sel_name1' disabled='true'>"+
			         "<c:forEach items='${keyword}' var='key' varStatus='i' >"+
			          "<option value='${key.id}+${key.keywordname}+${key.type}'>${key.keywordname}</option>"+
			          "</c:forEach>"+
		           "</select>"+
		           "    <select name='rules[0].nexus' class='input-xlarge required' id='sel_nexus1' disabled='true'>"+
		                "<option  value='0'></option>"+
		                "<option  value='1'>>=</option>"+
					    "<option  value='2'>&lt;=</option>"+
					    "<option  value='3'>=</option>"+
					    "<option  value='4'>包含</option>"+
					    "<option  value='5'>不包含</option>"+
		           "<select>"+
		           "  <input name='rules[0].ruleSet' htmlEscape='false' maxlength='20' class='input-xlarge required' id='sel_ruleSet1' readonly='true'/>"+
	               "</div>");
 	     $("#sel_name1").val(rul[0].keyId+"+"+rul[0].keyName+"+"+rul[0].keyType);
		 $("#sel_nexus1").val(rul[0].nexus);
		 $("#sel_ruleSet1").val(rul[0].ruleSet);
    }
    
    function audit(status){
    	var str='';
    	var pid='${aegisRuleSet.inputPid}';
    	var uid='${userId}';
    	if(pid==uid){
    		alert("审核人与录入人不能为同一人!");
    		return false;
    	}
    	if(status=='01'){
    		str="审核";
    	}else if(status=='02'){
    		str="打回";
    	}
    	
    	if(confirm("确定"+str+"规则集吗？")){	
    	    window.location.href="${ctx}/rule/aegisRuleSet/audit?id=${aegisRuleSet.id}&status="+status+"&branchId=${branchId}";
    	}
    }
    
    
    
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/rule/aegisRuleSet?branchId=${branchId}">规则集列表</a></li>
		<li class="active"><a href="${ctx}/rule/aegisRuleSet/form?id=${aegisRuleSet.id}">规则集<shiro:hasPermission name="rule:aegisRuleSet:edit">${not empty aegisRuleSet.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="rule:aegisRuleSet:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="aegisRuleSet" action="${ctx}/rule/aegisRuleSet/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<input type="hidden" id="sel_num" value="1"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">规则集名称：</label>
			<div class="controls">
				<form:input path="rulesetname" htmlEscape="false" maxlength="20" class="input-xlarge required" readonly="true"/>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">业务类型：</label>
			<div class="controls">
				<form:select path="businesstype" class="input-xlarge required"  disabled="true">
					<form:option value="1" label="金币业务"/>
					<form:option value="2" label="其他业务"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
			<label class="control-label">子集关系：</label>
			<div class="controls">
				<form:select path="relation" class="input-xlarge required" id="sel_relation" disabled="true">
					<form:option  value="1" label="或者"/>
					<form:option  value="2" label="并且"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group" id="d_rules">
			<label class="control-label">规则配置：</label>
			<p><div class="controls" id="1">
				<select name="rules[0].keyName" class="input-xlarge required" id="sel_name1">
				  <c:forEach items="${keyword}" var="key" varStatus="i">				  
			        <option  value="${key.id}+${key.keywordname}">${key.keywordname}</option>
			      </c:forEach>
				<select>
				<select name="rules[0].nexus" class="input-xlarge required" id="sel_nexus1">
					<option  value="1">>=</option>
					<option  value="2">&lt;=</option>
					<option  value="3">=</option>
					<option  value="4">包含</option>
					<option  value="5">不包含</option>
				</select>
				<input name="rules[0].ruleSet" htmlEscape="false" maxlength="20" class="input-xlarge required" id="sel_ruleSet1" />
			   </div></p>
		</div>
		<div class="control-group">
			<label class="control-label">结果预览</label>
			<div class="controls">
				<form:textarea path="remarks" htmlEscape="false" rows="4" maxlength="500" class="input-xxlarge" readonly="true" id="input_remarks"/>
			</div>
		</div>
		<div class="form-actions">
		   <c:if test="${mark==2}">
			   <shiro:hasPermission name="rule:aegisRuleSet:edit"><input id="btnSubmit" class="btn btn-primary" type="button" value="审核" onclick="audit('01')" />&nbsp;</shiro:hasPermission>
			   <shiro:hasPermission name="rule:aegisRuleSet:edit"><input id="btnSubmit" class="btn btn-primary" type="button" value="打回" onclick="audit('02')"/>&nbsp;</shiro:hasPermission>
			</c:if>
		</div>
	</form:form>
</body>
</html>